const host = 'http://localhost:8000'

function sel() {
  document.querySelectorAll('.booklist > tbody')[1].remove()
  init()

}

function add() {
  location.href = './bookAddOrEdit.html'
}

function edit(id) {
  location.href = `./bookAddOrEdit.html?id=${id}`
}

function del(id) {
  if (confirm('你确定要删除吗？')) {
    axios.delete(`${host}/book/${id}`).then(res => {
      if (res.data.code === 200) {
        alert('删除成功')
        location.href = './book.html'
      }
    })
  }
}

function init() {
  const keyword = document.querySelector('.keyword').value.trim()
  axios.get(`${host}/book?keyword=${keyword}`).then(res => {
    const data = res.data.data
    const booklist = document.querySelector('.booklist')
    booklist.innerHTML += data.map(item =>
      `
        <tr>
          <td>${item.id}</td>
          <td>${item.bookname}</td>
          <td>${item.author}</td>
          <td>${item.descriptions}</td>
          <td>
            <input type="button" value="编辑" onclick="edit(${item.id})">
            <input type="button" value="删除" onclick="del(${item.id})">
          </td>
        </tr>
      `
    ).join('')
  })
}
init()